package com.ysd.lis.service.qi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabBarCodeType;
import com.ysd.lis.entity.qi.QiIndexItem;
import com.ysd.lis.entity.qi.QiIndexMain;
import com.ysd.lis.entity.qi.QiJobVsMainitem;
import com.ysd.lis.mapper.qi.QiIndexItemMapper;
import com.ysd.lis.mapper.qi.QiJobVsMainitemMapper;
import com.ysd.lis.request.qi.QiIndexItemVo;
import com.ysd.lis.request.qi.QiItemDto;
import com.ysd.lis.request.qi.QiJobVsMainitemVo;
import com.ysd.lis.service.qi.QiJobVsMainitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 任务与指标项目对照表 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-07-12
 */
@Service
public class QiJobVsMainitemServiceImpl extends ServiceImpl<QiJobVsMainitemMapper, QiJobVsMainitem> implements QiJobVsMainitemService {

    @Autowired
    private QiJobVsMainitemMapper qiJobVsMainitemMapper;
    @Autowired
    private QiIndexItemMapper qiIndexItemMapper;
    @Override
    public Result findQiJobVsMainitemList(QiJobVsMainitem qiJobVsMainitem) {
        MPJLambdaWrapper<QiJobVsMainitem> queryWrapper=new MPJLambdaWrapper<>();
        queryWrapper.selectAll(QiJobVsMainitemVo.class);
        queryWrapper.selectAs(QiIndexMain::getIndexName,QiJobVsMainitemVo::getIndexName);
        queryWrapper.selectAs(QiIndexItem::getItemName,QiJobVsMainitemVo::getItemName);
        queryWrapper.leftJoin(QiIndexMain.class,QiIndexMain::getId,QiJobVsMainitem::getIndexId);
        queryWrapper.leftJoin(QiIndexItem.class,QiIndexItem::getId,QiJobVsMainitem::getItemId);
        queryWrapper.eq(QiJobVsMainitem::getDelFlag,0);
        queryWrapper.eq(QiJobVsMainitem::getJobId,qiJobVsMainitem.getJobId());
        queryWrapper.orderByAsc(QiIndexMain::getSeq);
        queryWrapper.orderByAsc(QiIndexItem::getSeq);
        List<QiJobVsMainitemVo> qiJobVsMainitemVos = qiJobVsMainitemMapper.selectJoinList(QiJobVsMainitemVo.class, queryWrapper);
        return Result.succ(qiJobVsMainitemVos);
    }

    @Override
    public Result findQiItemList(QiItemDto qiItemDto) {
        MPJLambdaWrapper<QiIndexItem> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper.selectAll(QiIndexItemVo.class);
        mpjLambdaWrapper.selectAs(QiIndexMain::getIndexName,QiIndexItemVo::getIndexName);
        mpjLambdaWrapper.selectAs(QiIndexMain::getIndexCode,QiIndexItemVo::getIndexCode);
        mpjLambdaWrapper.leftJoin(QiIndexMain.class,QiIndexMain::getId,QiIndexItem::getIndexId);
        mpjLambdaWrapper.eq(LabBarCodeType::getDelFlag, 0);
        if (StrUtil.isNotEmpty(qiItemDto.getKeyWords())) {
            mpjLambdaWrapper.and(wrapper -> wrapper.like(QiIndexMain::getIndexName, qiItemDto.getKeyWords()).or().like(QiIndexItem::getItemName, qiItemDto.getKeyWords()));
        }
        StringBuffer existsSql =new StringBuffer("SELECT 1 FROM QI_JOB_VS_MAINITEM t2 WHERE t.ID = t2.ITEM_ID AND t2.JOB_ID = '"+qiItemDto.getJobId()+"' ") ;

        mpjLambdaWrapper.notExists(existsSql.toString());
        mpjLambdaWrapper.orderByAsc(QiIndexMain::getSeq);
        mpjLambdaWrapper.orderByAsc(QiIndexItem::getSeq);
        List<QiIndexItemVo> qiIndexItems = qiIndexItemMapper.selectJoinList(QiIndexItemVo.class,mpjLambdaWrapper);
        return Result.succ(1, "查询成功", qiIndexItems);
    }

    @Override
    public Result addQiJobVsMainitem(QiJobVsMainitem qiJobVsMainitem) {
        boolean save = this.save(qiJobVsMainitem);
        if (save) {
            return Result.succ(1, "", null);
        } else {
            return Result.fail("添加失败！");
        }
    }

    @Override
    public Result deleteQiJobVsMainitem(QiJobVsMainitem qiJobVsMainitems) {
        int i = qiJobVsMainitemMapper.deleteById(qiJobVsMainitems.getId());
        if (i > 0) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
